#include <QMessageBox>
#include <QFileDialog>
#include "DialogNewMap.hpp"
#include "ui_DialogNewMap.h"
#include "Is.hpp"

DialogNewMap::DialogNewMap(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogNewMap),
    _loadFromFile(false),
    _createNewMap(false),
    _mapWidth(0),
    _mapHeight(0)
{
    ui->setupUi(this);

    initWidgets();
}

DialogNewMap::~DialogNewMap()
{
    delete ui;
}

void    DialogNewMap::initWidgets()
{
    QObject::connect(ui->pushButtonCreate, SIGNAL(clicked()), this, SLOT(buttonActionNewMap()));
    QObject::connect(ui->pushButtonCancel, SIGNAL(clicked()), this, SLOT(close()));

    QObject::connect(ui->radioButtonLoad, SIGNAL(toggled(bool)), this, SLOT(radioButtonActionLoad(bool)));
    QObject::connect(ui->radioButtonCreate, SIGNAL(toggled(bool)), this, SLOT(radioButtonActionCreate(bool)));

    QObject::connect(ui->pushButtonBrowse, SIGNAL(clicked()), this, SLOT(buttonActionBrowse()));
}


void    DialogNewMap::buttonActionNewMap()
{
    if (ui->radioButtonLoad->isChecked())
    {
        if (ui->lineEditFilePath->text() != "")
        {
            _mapPath = ui->lineEditFilePath->text();
            _loadFromFile = true;
            this->close();
        }
        else
            QMessageBox::critical(this, "Error", "Please load a file.");
    }
    else
    {
        QString mapWidth = ui->lineEditMapWidth->text();
        QString mapHeight = ui->lineEditMapWidth->text();

        if (mapWidth != "" && mapHeight != "" && Is::isNum(mapWidth) && Is::isNum(mapHeight))
        {
            _mapWidth = mapWidth.toInt();
            _mapHeight = mapHeight.toInt();
            _createNewMap = true;
            this->close();
        }
        else
            QMessageBox::critical(this, "Error", "Please fill the fields tile width and tile height with integer values.");
    }
}

void    DialogNewMap::radioButtonActionLoad(bool isPressed)
{
    if (isPressed == true)
    {
        ui->lineEditFilePath->setEnabled(true);
        ui->pushButtonBrowse->setEnabled(true);
    }
    else
    {
        ui->lineEditFilePath->setEnabled(false);
        ui->pushButtonBrowse->setEnabled(false);

    }
}

void    DialogNewMap::radioButtonActionCreate(bool isPressed)
{
    if (isPressed == true)
    {
        ui->labelDimension->setEnabled(true);
        ui->labelMapWidth->setEnabled(true);
        ui->labelMapHeight->setEnabled(true);
        ui->lineEditMapWidth->setEnabled(true);
        ui->lineEditMapHeight->setEnabled(true);
    }
    else
    {
        ui->labelDimension->setEnabled(false);
        ui->labelMapWidth->setEnabled(false);
        ui->labelMapHeight->setEnabled(false);
        ui->lineEditMapWidth->setEnabled(false);
        ui->lineEditMapHeight->setEnabled(false);
    }
}

void    DialogNewMap::buttonActionBrowse()
{
    QString	result = QFileDialog::getOpenFileName(this, QString("Load heightmap..."), QString("."), QString("Image (*.png *.jpg *.bmp)"));

    if (result != "")
    {
        ui->lineEditFilePath->setText(result);
    }
}

bool    DialogNewMap::loadMapFromFile() const
{
    return _loadFromFile;
}

bool    DialogNewMap::createNewMap() const
{
    return _createNewMap;
}

QString const&  DialogNewMap::getMapPath() const
{
    return _mapPath;
}


int     DialogNewMap::getMapWidth() const
{
    return _mapWidth;
}

int     DialogNewMap::getMapHeight() const
{
    return _mapHeight;
}


